import 'package:fish_redux/fish_redux.dart';

import 'action.dart';
import 'state.dart';

Reducer<MainViewState> buildReducer() {
  return asReducer(
    <Object, Reducer<MainViewState>>{
      MainViewAction.action: _onAction,
      MainViewAction.onGetData: _onGetData,
      MainViewAction.setSort: _setSort,
      MainViewAction.onSetItems: _onSetItems,
      MainViewAction.onSetMultipleSelect: _onSetMultipleSelect,
      MainViewAction.onSelectAll: _onSelectAll,
      MainViewAction.onSelectOne: _onSelectOne,
    },
  );
}

MainViewState _onAction(MainViewState state, Action action) {
  final MainViewState newState = state.clone();
  return newState;
}

MainViewState _onGetData(MainViewState state, Action action) {
  final MainViewState newState = state.clone();
  if (state.userInfo.isCountLeader) {
    newState.countModel = action.payload;
    newState.refreshController.finishRefresh();
  } else {
    if (state.userInfo.isInstallWorker) {
      newState.installModel = action.payload;
    } else
      newState.model = action.payload;
  }
  return newState;
}

MainViewState _setSort(MainViewState state, Action action) {
  final MainViewState newState = state.clone()
    ..sortIndex = action.payload
    ..multipleSelect = false;
  if (newState.userInfo.isCheckLeader) {
    if (action.payload == 1) {
      newState.items.sort((a,b){
        double checkValueA = a.totalNumber == 0
            ? 0
            : a.checkedNumber / a.totalNumber;
        double checkValueB= b.totalNumber == 0
            ? 0
            : b.checkedNumber / b.totalNumber;
        return checkValueB.compareTo(checkValueA);
      });
    }
    if (action.payload == 2) {
      newState.items.sort((a,b){
        double commonValueA = a.totalNumber == 0
            ? a.totalNumber.toDouble()
            : a.normalNumber / a.totalNumber;

        double commonValueB = b.totalNumber == 0
            ? b.totalNumber.toDouble()
            : b.normalNumber / b.totalNumber;

        return commonValueB.compareTo(commonValueA);
      });
    }
  }
  else {
    newState.items = [];
    newState.itemLoaded = false;
  }
  newState.selectItems.clear();
  return newState;
}

MainViewState _onSetItems(MainViewState state, Action action) {
  int page = action.payload['page'];
  List items = action.payload['items'];
  final MainViewState newState = state.clone();
  if (items == null) {
    newState.currentPage = page;
    newState.refreshController.finishLoad(noMore: true);
    newState.refreshController.finishRefresh();
    newState.items = items;
  } else {
    if (page == 1) {
      newState.items = items;
      newState.refreshController.finishRefresh();
    } else {
      List tmp = [];
      tmp.addAll(newState.items);
      tmp.addAll(items);
      newState.items = tmp;
    }
    newState.currentPage = page;
    newState.itemLoaded = true;
    newState.refreshController.finishLoad(noMore: items.length == 0);
  }

  return newState;
}

MainViewState _onSetMultipleSelect(MainViewState state, Action action) {
  final MainViewState newState = state.clone()
    ..multipleSelect = !state.multipleSelect;
  newState.selectItems.clear();
  return newState;
}

MainViewState _onSelectAll(MainViewState state, Action action) {
  final MainViewState newState = state.clone();
  if (newState.selectItems.length == newState.items.length)
    newState.selectItems.clear();
  else {
    newState.selectItems.clear();
    newState.selectItems.addAll(newState.items);
  }
  return newState;
}

MainViewState _onSelectOne(MainViewState state, Action action) {
  final MainViewState newState = state.clone();
  if (newState.selectItems.contains(action.payload))
    newState.selectItems.remove(action.payload);
  else
    newState.selectItems.add(action.payload);
  return newState;
}
